package com.aaa.bbb.mymaintain.dao;

import com.aaa.bbb.mymaintain.entity.IssuesMaintenance;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author max
 * @since 2023-11-03
 */
public interface IssuesMaintenanceDao extends BaseMapper<IssuesMaintenance> {

    @Select({
            "<script> " +
                    "SELECT "   +
                    "    issues_employee.issue_id, "   +
                    "    issues_employee.job_id, "   +
                    "    employee.name, "   +
                    "    issues_employee.detail, "   +
                    "    issues_employee.picture, "   +
                    "    issues_employee.state, "   +
                    "    explanation, "   +
                    "    issues_employee.date "   +
                    "FROM   issues_employee"   +
                    "   INNER JOIN employee ON issues_employee.job_id = employee.job_id "   +
                    "WHERE  1=1 " +
                    "  <if test='maintenance_id != null and maintenance_id != \"\" '>" +
                    "     AND maintenance_id = #{maintenance_id} " +
                    "  </if>"   +
                    "  <if test='state != null and state != \"\" '>" +
                    "     AND FIND_IN_SET(state,#{state})" +
                    "  </if>"   +
                    "ORDER BY FIELD(issues_employee.state, '维修不合格', '经费申请未通过', '待申请经费', '经费申请中') ,issues_employee.issue_id DESC " +
                    "</script>"
    })
    public List<Map<String, Object>> selectJudge(@Param("maintenance_id")String maintenance_id,@Param("state")String state);


    @Update(
            "<script> " +
                    "UPDATE issues_employee SET state=#{state} "  +
                    "  <if test='reason != null and reason != \"\" '>" +
                    ", applyfund_reason = #{reason} "  +
                    "  </if>"   +
                    "WHERE "  +
                    "issue_id = #{issue_id} " +
                    "</script>"
    )
    public void updateApplyReason(@Param("issue_id") String issue_id, @Param("state")String state, @Param("reason")String reason);


    @Update(
            "<script> " +
                    "UPDATE issues_employee SET state=#{state} "  +
                    "  <if test='reason != null and reason != \"\" '>" +
                    ", cannot_repair_reason = #{reason} "  +
                    "  </if>"   +
                    "WHERE "  +
                    "issue_id = #{issue_id} " +
                    "</script>"
    )
    public void updateCannotRepairReason(@Param("issue_id") String issue_id, @Param("state")String state, @Param("reason")String reason);


    @Select({
            "<script> " +
                    "SELECT "   +
                    "   job_id "   +
                    "FROM   employee "   +
                    "WHERE  1=1 " +
                    "  <if test='username != null and username != \"\" '>" +
                    "     AND username = #{username} " +
                    "  </if>"   +
                    "</script>"
    })
    public String selectJobId(@Param("username")String username);
}
